package com.dyzx.hbb.modules.sys.controller;

import com.dyzx.hbb.common.base.Result;
import com.dyzx.hbb.common.config.MyLambdaQueryWrapper;
import com.dyzx.hbb.modules.mtn.entity.MtnOrder;
import com.dyzx.hbb.modules.mtn.service.MtnOrderService;
import com.dyzx.hbb.modules.sys.bean.SysWorkPaymentBean;
import com.dyzx.hbb.modules.sys.entity.SysWorkFlow;
import com.dyzx.hbb.modules.sys.service.SysWorkPaymentService;
import com.dyzx.hbb.modules.sys.vo.SysWorkPaymentBeanUVO;
import com.dyzx.hbb.modules.sys.vo.SysWorkPaymentBeanVO;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/sys/work/payment")
public class SysWorkPaymentController {
    @Autowired
    private SysWorkPaymentService service;

    @Autowired
    private MtnOrderService mtnOrderService;


    @GetMapping("/list")
    public Result<SysWorkPaymentBean> list(
            @RequestParam(value = "order_id", defaultValue = "0", required = true) Long orderId,
            @RequestParam(value = "type", defaultValue = "0", required = true) Integer type
    ) {
        if (orderId <= 0) {
            return Result.error(400, "参数错误");
        }
        if (type <= 0) {
            return Result.error(400, "参数错误");
        }
        SysWorkPaymentBean workPayment = service.getWorkPayment(orderId, type);
        return Result.ok(workPayment);
    }

    /*        "payment_method":paymentMethod,
                "order_id":orderId,
                "money":paymentMoney*/
    @PostMapping
    public Result<Boolean> save(@Valid @RequestBody SysWorkPaymentBeanVO vo) {
        boolean save = service.savePayment(vo);
        return save ? Result.ok() : Result.error(400, "保存失败");

    }

    @PutMapping
    public Result<Boolean> update(@Valid @RequestBody SysWorkPaymentBeanUVO vo) {
        boolean save = service.endPayment(vo);
        return save ? Result.ok() : Result.error(400, "保存失败");
    }


    @GetMapping("/mini_order_list")
    public Result<List<Map<String, Object>>> miniList() {
        MyLambdaQueryWrapper<MtnOrder> queryWrapper = new MyLambdaQueryWrapper<>();
        queryWrapper.select(MtnOrder::getId).as("value");
        queryWrapper.select(MtnOrder::getNo).as("label");
        queryWrapper.in(MtnOrder::getStatus, 1, 2);
        queryWrapper.orderByDesc(MtnOrder::getId);
        List<Map<String, Object>> maps = mtnOrderService.listMaps(queryWrapper);
        if (maps.isEmpty()) {
            return Result.error(404, "暂无数据");
        }
        return Result.ok(maps);
    }
}
